package com.forestar.mapControl.utils.mapConvert.task;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Paint;

import com.forestar.mapControl.mapview.CacheCanvas;
import com.forestar.mapControl.mapview.multitouch.TestTextureViewRefreshListener;
import com.forestar.mapControl.utils.LogD;

import java.io.File;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * created by  xzk
 * 2020/8/5   12:03
 * 说明:
 */
public abstract class DrawForTask implements Runnable {

    public static int tileSize = 256;
    public static int tileSizeForShow = 128;

    public static int offsetNum = 14;
    public static int offsetNumY = 14;
    public static int offsetSizeTwice = 50;

    //上下左右新增屏幕的1/2 试试看效果
    //width = 1600.0,height = 2560.0  25个一横   40个一纵
    public static int widthScreen = 1600, heightScreen = 2560;

    protected List<String> millSum;
    protected CacheCanvas targetCanvas;
    protected List<CacheCanvas> cacheCanvasList;
    protected List<Bitmap> bitmapTempList;


    protected Paint tempPaint = new Paint();
    protected Paint textPaint = new Paint();
    protected Paint boxPaint = new Paint();

    protected TestTextureViewRefreshListener refreshListener;
    protected AtomicInteger hasTaskNum;

    public DrawForTask(TestTextureViewRefreshListener refreshListener, AtomicInteger hasTaskNum) {
        textPaint.setColor(Color.BLUE);
        textPaint.setTextSize(50);
        textPaint.setStrokeWidth(5);

        boxPaint.setColor(Color.RED);
        boxPaint.setStyle(Paint.Style.STROKE);
        boxPaint.setStrokeWidth(2);
        this.refreshListener = refreshListener;
        this.hasTaskNum = hasTaskNum;
    }

    public List<String> getMillSum() {
        return millSum;
    }

    public void setTargetCanvas(CacheCanvas targetCanvas) {
        this.targetCanvas = targetCanvas;
    }

    public void setCacheCanvasList(List<CacheCanvas> cacheCanvasList) {
        this.cacheCanvasList = cacheCanvasList;
    }

    public void setBitmapTempList(List<Bitmap> bitmapTempList) {
        this.bitmapTempList = bitmapTempList;
    }

    //获取文件夹下所有的文件
    protected void getAllTileFromFile() {
        bitmapTempList.clear();
        String tempFileDirectory = LoadFileTileTask.buildTempFileDirectory();
        File t1 = new File(tempFileDirectory);
        File[] files = t1.listFiles();
        for (File file : files) {
            if (!file.isDirectory()) {
                Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                bitmapTempList.add(bitmap);
            }
        }
    }

    @Override
    public void run() {
        long start = System.currentTimeMillis();
        int allNum = drawBitmapTask();
        long end = System.currentTimeMillis();
        LogD.d("瓦片文件数量：总计："+allNum+"<-- 渲染用时-->"+(end-start));
        millSum.add("瓦片文件数量：总计："+allNum+"<--渲染用时->"+(end-start));

        if(null != refreshListener){
            refreshListener.refreshTextureView();
            try {
                Thread.sleep(14);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //以原子的方式当前值 -1
        hasTaskNum.decrementAndGet();
    }
    public abstract int drawBitmapTask();
}
